// pages/changePatient/changePatient.js
var IDCard = require('./idCard.js')
var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; //验证身份证
Page({
  data: {
    patientInfo: {
      admin: '',
      isAdded: false,
      patientName: '',
      patientId: '',
      patientAge: null,
      patientSex: '',
      patientPhone: '',
      patientRegion: ['广东省', '江门市', '蓬江区'],
      patientAddress: '',
      patientAller: '',
      patientHistory: '',
    },
  },
  //一进来的时候获取数据
  onLoad(options) {
    var that = this
    console.log(options.admin)
    //一进来就获取这个人的账号
    var admin = 'patientInfo.admin'
    that.setData({
      [admin]: options.admin
    })
    //利用账号，调用云函数查看这个账号的信息，如果有这个人的信息就展示出来，如果没有就不展示
    wx.cloud.callFunction({
      name: "patientInfo",
      data: {
        action: "findOne",
        admin: options.admin
      }
    }).then(res => {
      console.log('调用云函数成功', res)
      //把数据库拿到的数据存放到本地
      if (res.result.data[0]) { //如果有数据，就存到本地
        that.setData({
          patientInfo: res.result.data[0]
        })
      }
    }).catch(res => {
      '调用云函数失败',
      res
    })
  },
  //获取数据
  getpatientName(e) {
    var patientName = 'patientInfo.patientName'
    this.setData({
      [patientName]: e.detail.value
    })
  },
  getpatientId(e) {
    var that = this
    var patientId = 'patientInfo.patientId'
    var patientAge = 'patientInfo.patientAge'
    var patientSex = 'patientInfo.patientSex'
    this.setData({
      [patientId]: e.detail.value,
    })
    if (reg.test(e.detail.value)) {
      that.setData({
        [patientAge]: IDCard.IdCard(e.detail.value, 3),
        [patientSex]: IDCard.IdCard(e.detail.value, 2)
      })
    }
  },
  getpatientPhone(e) {
    var patientPhone = 'patientInfo.patientPhone'
    this.setData({
      [patientPhone]: e.detail.value
    })
  },
  getuserRegin: function (e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    var patientRegion = "patientInfo.patientRegion"
    this.setData({
      [patientRegion]: e.detail.value
    })
  },
  getpatientAddress(e) {
    var patientAddress = 'patientInfo.patientAddress'
    this.setData({
      [patientAddress]: e.detail.value
    })
  },
  getpatientAller(e) {
    var patientAller = 'patientInfo.patientAller'
    this.setData({
      [patientAller]: e.detail.value.replace(/\s+/g, '')
    })
  },
  getpatientHistory(e) {
    var patientHistory = 'patientInfo.patientHistory'
    this.setData({
      [patientHistory]: e.detail.value.replace(/\s+/g, '')
    })
  },
  //提交数据
  commit() {
    var that = this
    wx.showLoading({
      title: that.data.patientInfo.isAdded ? '我的信息修改中...' : '我的信息新建中...',
    })
    //验证必填
    if (!that.data.patientInfo.patientName) {
      wx.showToast({
        icon: 'none',
        title: '姓名不能为空',
      })
      return
    }
    if (!that.data.patientInfo.patientId) {
      wx.showToast({
        icon: 'none',
        title: '身份证号不能为空',
      })
      return
    }
    if (!that.data.patientInfo.patientPhone) {
      wx.showToast({
        icon: 'none',
        title: '电话号码不能为空',
      })
      return
    }
    //验证身份证
    if (!reg.test(that.data.patientInfo.patientId)) {
      wx.showToast({
        icon: 'none',
        title: '身份证格式有误',
      })
      return
    }
    //验证手机号
    var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)|^[1][3-8]\d{9}$|^([6|9])\d{7}$|^[6]([8|6])\d{5}$/;
    if (!phoneReg.test(that.data.patientInfo.patientPhone)) {
      wx.showToast({
        icon: 'none',
        title: '手机号码格式有误',
      })
      return
    }
    //提交
    if (that.data.patientInfo.isAdded) { //如果添加过了就调用修改函数
      that.commitC()
    } else {
      that.commitA() //如果没有添加过就调用新建函数
    }
  },
  commitA() { //新建函数
    var that = this
    wx.cloud.callFunction({
      name: 'patientInfo',
      data: {
        action: 'add',
        isAdded: true,
        admin: that.data.patientInfo.admin,
        patientName: that.data.patientInfo.patientName,
        patientId: that.data.patientInfo.patientId,
        patientAge: that.data.patientInfo.patientAge,
        patientSex: that.data.patientInfo.patientSex,
        patientPhone: that.data.patientInfo.patientPhone,
        patientRegion: that.data.patientInfo.patientRegion,
        patientAddress: that.data.patientInfo.patientAddress,
        patientAller: that.data.patientInfo.patientAller,
        patientHistory: that.data.patientInfo.patientHistory,
      }
    }).then(res => {
      console.log('新建个人信息成功', res)
      wx.hideLoading({
        success: (res) => {
          wx.showToast({
            title: '新建信息成功',
          })
        },
      })
      setTimeout(function () {
        wx.navigateBack({})
      }, 1000)
    }).catch(res => {
      console.log('新建个人信息失败', res)
      wx.hideLoading({
        success: (res) => {
          wx.showToast({
            icon: 'none',
            title: '新建信息失败',
          })
        },
      })
    })
  },
  commitC() { //修改函数
    var that = this
    wx.cloud.callFunction({
      name: 'patientInfo',
      data: {
        action: 'change',
        admin:that.data.patientInfo.admin,
        patientName: that.data.patientInfo.patientName,
        patientId: that.data.patientInfo.patientId,
        patientAge: that.data.patientInfo.patientAge,
        patientSex: that.data.patientInfo.patientSex,
        patientPhone: that.data.patientInfo.patientPhone,
        patientRegion: that.data.patientInfo.patientRegion,
        patientAddress: that.data.patientInfo.patientAddress,
        patientAller: that.data.patientInfo.patientAller,
        patientHistory: that.data.patientInfo.patientHistory,
      }
    }).then(res => {
      console.log('修改个人信息成功', res)
      wx.hideLoading({
        success: (res) => {
          wx.showToast({
            title: '修改信息成功',
          })
        },
      })
      setTimeout(function () {
        wx.navigateBack({})
      }, 1000)
    }).catch(res => {
      console.log('修改个人信息失败', res)
      wx.hideLoading({
        success: (res) => {
          wx.showToast({
            icon: 'none',
            title: '修改信息失败',
          })
        },
      })
    })
  }
})